﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HgCo.WindowsLive.SkyDrive;
using System.IO;

namespace Rung.SkyDriver
{
    public class SkyDriverHelper
    {
        private const string userName = "rungroo@gmail.com";
        private const string password = "7253685";
        SkyDriveServiceClient srvSkyDrive;

        public SkyDriverHelper()
        {
            this.srvSkyDrive = new SkyDriveServiceClient();
        }

        public void Login()
        {
            srvSkyDrive.LogOn(userName, password); 
        }

        /* --------------------------------------------------------------------------------------------------------查询------------- */

        public WebDriveInfo GetDriveInfo()
        {
            return srvSkyDrive.GetWebDriveInfo();
        }

        public WebFolderInfo[] GetFolders()
        {
            return srvSkyDrive.ListRootWebFolders();
        }

        public WebFolderInfo[] GetFolders(WebFolderInfo folder)
        {
            return srvSkyDrive.ListSubWebFolders(folder);
        }

        public WebFileInfo[] GetFiles(WebFolderInfo folder)
        {
            return srvSkyDrive.ListSubWebFiles(folder);
        }

        /* --------------------------------------------------------------------------------------------------------创建------------- */

        public WebFolderInfo CreateFolder(string folderName)
        {
            return srvSkyDrive.CreateRootWebFolder(
                    folderName,
                    WebFolderCategoryType.Documents,
                    WebFolderItemShareType.Private);
        }

        public WebFolderInfo CreateFolder(string folderName, WebFolderInfo parent)
        {
            return srvSkyDrive.CreateSubWebFolder(folderName, parent);
        }

        /* --------------------------------------------------------------------------------------------------------删除------------- */

        public void DeleteFolder(WebFolderInfo folder)
        {
            srvSkyDrive.DeleteWebFolder(folder);
        }

        public void DeleteFile(WebFileInfo file)
        {
            srvSkyDrive.DeleteWebFile(file);
        }

        /* --------------------------------------------------------------------------------------------------------上传下载--------- */

        public WebFileInfo Upload(string fileName, WebFolderInfo folder)
        {
            return srvSkyDrive.UploadWebFile(fileName, folder);
        }

        public Stream Download(WebFileInfo file)
        {
            return srvSkyDrive.DownloadWebFile(file);
        }

        public void Download(WebFileInfo file, string saveFileName)
        {
            using (Stream s = srvSkyDrive.DownloadWebFile(file))
            {
                using (FileStream sw = new FileStream(saveFileName, FileMode.Create))
                {
                    int length = 0;
                    byte[] data=new byte[1024];

                    while ((length = s.Read(data, 0, 1024)) > 0)
                    {
                        sw.Write(data, (int)sw.Position, length);
                    }
                }
            }
        }

        public void ChangeFolderDescription(WebFolderInfo folder, string text)
        {
            throw (new NotImplementedException());
        }
    }
}
